package VeLaBai;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

class ve extends JPanel implements Runnable {
	
	static private String craft = "b2fv.png";
	private Image image;
	private final int DELAY = 50;
	private Thread animator;
    private int x, y;
	
	public ve(){
		//setDoubleBuffered(true);
ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
		
		image = ii.getImage();
		x = 400;
		y = 350;
	}
	

	@Override
	public void paint(Graphics g) {
		// TODO Auto-generated method stub
		super.paint(g);
		//for(int i = 0 ; i < 100;i=i+10)
		//{
		Graphics2D g2d = (Graphics2D)g;
        g2d.drawImage(image, 400, 350, this);
		//}
        
        g2d.drawImage(image, x, y, this);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
       
	}
	 public void addNotify() {
	        super.addNotify();
	        animator = new Thread(this);
	        animator.start();
	    }
	//goc trai duoi	 
/*	 public void cycle() {

	        x -= 100;
	        y += 100;

	        if (y > 700) {
	            y = 350;
	            x = 400;
	        }
	    }*/
	 // goc phai duoi
//	 public void cycle() {
//
//	        x += 100;
//	        y += 100;
//
//	        if (y > 700) {
//	            y = 350;
//	            x = 400;
//	        }
//	    }
//goc trai tren
//	 public void cycle() {
//
//	        x -= 100;
//	        y -= 100;
//
//	        if (y < 50) {
//	            y = 350;
//	            x = 400;
//	        }
//	    }
	 //goc phai tren
	 public void cycle() {

	        x += 10;
	        y -= 10;

	        if (y < 50) {
	        	
	            y = 350;
	            x = 400;
	        }
	        
	    }

	    public void run() {
	    	

	        long beforeTime, timeDiff, sleep;

	        beforeTime = System.currentTimeMillis();

	        while (true) {

	            cycle();
	            
	            repaint();

	            timeDiff = System.currentTimeMillis() - beforeTime;
	            sleep = DELAY - timeDiff;

	            if (sleep < 0)
	                sleep = 2;
	            try {
	                Thread.sleep(sleep);
	            } catch (InterruptedException e) {
	                System.out.println("interrupted");
	            }

	            beforeTime = System.currentTimeMillis();
	        }
	    }
}
